package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.OrderBill;
import cn.wolfcode.crm.domain.OrderBillItem;
import cn.wolfcode.crm.mapper.OrderBillItemMapper;
import cn.wolfcode.crm.mapper.OrderBillMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IOrderBillService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OrderBillServiceImpl implements IOrderBillService{
    @Autowired
    private OrderBillMapper mapper;
    @Autowired
    private OrderBillItemMapper  itemMapper;
    public void save(OrderBill entity) {
        //添加订单
        //设置时间
        entity.setInputTime(new Date());
        //设置录入人
        Employee username = new Employee();
        username.setId(1L);
        entity.setSn(System.currentTimeMillis()+""); //生成毫秒数

        entity.setInputUser(username);
        //设置
        entity.setStatus(OrderBill.STATUS_QUIT);
        BigDecimal totalAmount = BigDecimal.ZERO; //设置当前总计为0
        BigDecimal totalNumber = BigDecimal.ZERO; //设置当前数量为0
        //添加明细,和计算订单总额
        List<OrderBillItem> items = entity.getItems();
        for (OrderBillItem item : items) {
                totalAmount = totalAmount.add(item.getCostPrice().multiply(item.getNumber()));
                totalNumber = totalNumber.add(item.getNumber());
        }
        entity.setTotalAmount(totalAmount);
        entity.setTotalNumber(totalNumber);
        mapper.insert(entity);

        //保存订单明细
        for (OrderBillItem item : items) {
            item.setBillId(entity.getId());
            item.setAmount(item.getCostPrice().multiply(item.getNumber()));
            itemMapper.insert(item);
        }

    }

    public void update(OrderBill entity) {
        //修改账单明细,先判断审核状态是否为未审核,已审核的不能更新
        OrderBill orderBill = mapper.selectByPrimaryKey(entity.getId());
        if (orderBill.getStatus()==OrderBill.STATUS_QUIT){
            //获取页面传输过来的明细
            List<OrderBillItem> items = entity.getItems();
            //先删除老的明细,在添加新的明细
            itemMapper.deleteByBillId(entity.getId());
            //获取账单的总金额和总数量
            BigDecimal totalAmount = BigDecimal.ZERO;
            BigDecimal totalNumber = BigDecimal.ZERO;
            for (OrderBillItem item : items) {
                totalAmount = totalAmount.add(item.getCostPrice().multiply(item.getNumber()));
                totalNumber = totalNumber.add(item.getNumber());
                //设置账单中的明细
                item.setBillId(entity.getId());
                item.setAmount(item.getCostPrice().multiply(item.getNumber()));
                itemMapper.insert(item);
            }
            entity.setTotalNumber(totalNumber);
            entity.setTotalAmount(totalAmount);
            mapper.updateByPrimaryKey(entity);
        }

    }

    public void delete(Long id) {
        itemMapper.deleteByBillId(id);
        mapper.deleteByPrimaryKey(id);
    }

    public List<OrderBill> list() {
        return mapper.selectAll();
    }

    public OrderBill get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

	public PageResult query(QueryObject qo) {
		return PageUtils.query(qo, mapper);
	}

    public void changeAudit(Long id) {
        //获取账单
        OrderBill orderBill = mapper.selectByPrimaryKey(id);
        //判断当前账单是否审核,没有审核的才能审核
        if (orderBill.getStatus()==OrderBill.STATUS_QUIT){
            //设置审核人,审核时间,审核状态
            Employee username = new Employee();
            username.setId(1L);

            orderBill.setAuditor(username);
            orderBill.setAuditTime(new Date());
            orderBill.setStatus(OrderBill.STATUS_EXIST);
            mapper.changeAudit(orderBill);
        }
    }


}
